<template>
  <a-spin :spinning="loading">
    <div class="page-container is-form" v-if="form != null">
      <a-form :form="form">
        <a-form-item :label="form.name" v-bind="formItemLayout">
          <a-radio-group v-decorator="[ 'value', { rules: [{ required: true, message: '请选择' }] } ]">
            <a-radio :value="1">是</a-radio>
            <a-radio :value="0">否</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item label="说明" v-bind="formItemLayout">
          <a-textarea  :autosize="{ minRows: 8, maxRows: 16 }"
                       v-decorator="[ 'remark', { rules: [{ required: true, message: '请填写内容' }] } ]" />
        </a-form-item>
        <a-form-item label=" ">
          <a-button type="primary" @click="onSave" :loading="isLoading">保存</a-button>
        </a-form-item>
      </a-form>
    </div>
  </a-spin>
</template>

<script>
  import {findListByType,saveDetail} from "../../../api/pubData";
  export default {
    name: "PageSet",
    props:{
      title:{
        type:String,
        default:'开关'
      },
      dataType:Object
    },
    data(){
      return{
        form:this.$form.createForm(this),
        formItemLayout: {
          labelCol: { span:5},
          wrapperCol: { span:18 },
        },
        params:{
          type:null,
          page: 1,
          pageSize: 1
        },
        model:{},
        loading:false,
        isLoading:false
      }
    },
    methods:{
      onSave(e) {
        e.preventDefault();
        this.isLoading = true;
        this.form.validateFields((err, values) => {
          if (!err) {
            this.handleSave(values)
          }
          this.isLoading = false;
        });
      },
      async handleSave(data){
        let form = {...this.model,...data,dataType:this.dataType.id,name:this.dataType.name}
        let resp = await saveDetail(form);
        this.isLoading = false;
        if(!this.qUtil.validResp(resp)){
          this.$message.error(resp.errMsg|| '保存失败');
          return;
        }
        this.$message.success("保存成功!");
        this.loadData();
      },
      async loadData(){
        this.loading = true;
        let resp = await findListByType(this.dataType.id);
        if(this.qUtil.validResp(resp)){
          let dataList = resp.data.dataList || []
          if(dataList.length>0){
            let form = dataList[0];
            this.model = form
            this.form.setFieldsValue({ value:form.value,remark:form.remark})
          }
        }
        this.loading = false;
      }
    },
    mounted() {
      this.model={}
      if(this.dataType != null) {
        this.loadData()
      }
    }
  }
</script>

<style>
.pageset .el-form-item .el-form-item__label{
  line-height: 40px;
}
</style>